package cn.itcast.springmvc.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.itcast.springmvc.exception.CustomerException;
import cn.itcast.springmvc.pojo.Items;
import cn.itcast.springmvc.pojo.QueryVo;
import cn.itcast.springmvc.service.ItemService;

import org.springframework.web.servlet.ModelAndView;

import com.sun.tools.internal.ws.wsdl.document.jaxws.Exception;

@Controller
//@RequestMapping("/item")
public class ItemsController {

	@Autowired
	private ItemService itemService;
	
	@RequestMapping(value="/itemList.action",method={RequestMethod.GET,RequestMethod.POST})
	public ModelAndView getItemList(){
		List<Items> itemsList = itemService.getItemList();
		ModelAndView view = new ModelAndView();
		view.setViewName("itemList");
		view.addObject("itemList", itemsList);
		return view;
	}
	
	
	/**
	 * 修改操作
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 * @throws CustomerException 
	 */
	/*@RequestMapping("/itemEdit.action")
	public ModelAndView skipToEdit(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		String parameter = request.getParameter("id");
		System.out.println(parameter);
		//通过id来查询items对象，然后跳转到editItem.jsp页面上去，将查询出来的数据带过去即可
		return view;
	}*/
	
	
	/*@RequestMapping("/itemEdit.action")
	public ModelAndView skipToEdit(Integer id){
		ModelAndView view = new ModelAndView();
		System.out.println(id);
		Items items = itemService.selectByPrimayKey(id);
		
		view.addObject("item", items);
		view.setViewName("editItem");
		//通过id来查询items对象，然后跳转到editItem.jsp页面上去，将查询出来的数据带过去即可
		return view;
	}*/
	
	
	//
	/*@RequestMapping("/itemEdit.action")
	public String skipToEdit(@RequestParam(value="ids" ,defaultValue="2",required=true) Integer id,Model model,ModelMap modelMap) throws CustomerException{
		System.out.println(id);
		//System.out.println(1/0);
		Items items = itemService.selectByPrimayKey(id);
		if(items == null){
			throw new CustomerException("请不要随便更改我的id");
		}
		model.addAttribute("item", items);
		model.addObject("item", items);
		model.setViewName("editItem");
		//通过id来查询items对象，然后跳转到editItem.jsp页面上去，将查询出来的数据带过去即可
		return "editItem";
	}*/
	/**
	 * restful风格的支持
	 * @param id
	 * @param model
	 * @param modelMap
	 * @return
	 * @throws CustomerException
	 */
	@RequestMapping("/itemEdit/{id}")
	public String skipToEdit(@PathVariable(value="id") Integer id,Model model,ModelMap modelMap) throws CustomerException{
		System.out.println(id);
		//System.out.println(1/0);
		Items items = itemService.selectByPrimayKey(id);
		if(items == null){
			throw new CustomerException("请不要随便更改我的id");
		}
		model.addAttribute("item", items);
		/*model.addObject("item", items);
		model.setViewName("editItem");*/
		//通过id来查询items对象，然后跳转到editItem.jsp页面上去，将查询出来的数据带过去即可
		return "editItem";
	}
	
	
	
	
	
	
	/**
	 * 修改操作，绑定pojo对象
	 * @throws IOException 
	 * @throws ServletException 
	 */
	/*@RequestMapping("/updateitem.action")
	public String updateItem(HttpServletRequest request,Items items){
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String detail = request.getParameter("detail");
		String id = request.getParameter("id");
		System.out.println(name+price+detail+"......."+id);
		System.out.println(items.toString());
		itemService.updateItem(items);
		return "success";
	}*/
	
	
	/**
	 * 方法的返回值是void的时候，如果做请求的转发
	 */
	/*@RequestMapping("/updateitem.action")
	public void updateItem(HttpServletRequest request,Items items,HttpServletResponse response) throws ServletException, IOException{
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String detail = request.getParameter("detail");
		String id = request.getParameter("id");
		System.out.println(name+price+detail+"......."+id);
		System.out.println(items.toString());
		itemService.updateItem(items);
		request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);
		//return "success";
	}*/
	
	/**
	 * 方法的返回值是void的时候，如果做请求的重定向
	 */
	/*@RequestMapping("/updateitem.action")
	public void updateItem(HttpServletRequest request,Items items,HttpServletResponse response) throws ServletException, IOException{
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String detail = request.getParameter("detail");
		String id = request.getParameter("id");
		System.out.println(name+price+detail+"......."+id);
		System.out.println(items.toString());
		itemService.updateItem(items);
		response.sendRedirect("http://localhost:8080/springAll2/itemList.action");
	//	request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);
		//return "success";
	}*/
	
	
	/**
	 * 返回值是string的时候，如何做请求的重定向
	 * @param request
	 * @param items
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
/*	@RequestMapping("/updateitem.action")
	public String updateItem(HttpServletRequest request,Items items,HttpServletResponse response) throws ServletException, IOException{
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String detail = request.getParameter("detail");
		String id = request.getParameter("id");
		System.out.println(name+price+detail+"......."+id);
		System.out.println(items.toString());
		itemService.updateItem(items);
		return "redirect:/itemList.action";
	}*/
	
	
	/*
	 * 请求转发，当方法的返回值是string的时候，如果做请求的转发
	 */
	@RequestMapping("/updateitem.action")
	public String updateItem(HttpServletRequest request,Items items,HttpServletResponse response,MultipartFile pictureFile) throws ServletException, IOException{
		String picName = pictureFile.getName();
		//获取文件的原始名称
		String originalFilename = pictureFile.getOriginalFilename();
		//abc.png
		String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
		String firstPart = UUID.randomUUID().toString();
		String totalName = firstPart+substring;
		File file  = new File("E:\\uploadPic"+File.separator+totalName);
		pictureFile.transferTo(file);
		items.setPic(totalName);
		
		System.out.println(picName);
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String detail = request.getParameter("detail");
		String id = request.getParameter("id");
		System.out.println(name+price+detail+"......."+id);
		System.out.println(items.toString());
		itemService.updateItem(items);
		return "forward:/itemList.action";
	}
	
	
	
	
	
	@RequestMapping("/queryitem.action")
	public String queryItem(QueryVo queryVo,String [] ids){
		
		System.out.println(queryVo.getItems().getName());
		
		System.out.println(queryVo.getItems().getDetail());
		
		return "success";
	}
	
	
	
	
	
	/**
	 * springMVC的json数据交互
	 */
	@RequestMapping("/ajaxJson.action")
	@ResponseBody
	public String ajaxJson(@RequestBody Items items){
		
		System.out.println(items.getName());
		
		return "abc";
	}
	
	
	/**
	 * 跳转到登录页面
	 */
	@RequestMapping("/skipToLogin.action")
	public String skipToLogin(){
		
		
		return "login";
	}
	
	
	
	
	@RequestMapping("/login.action")
	public String loginAction(String name,String password,HttpSession session){
		//如果登录成功，重定向到itemList.action
		//如果登录失败，重定向到登录页面
		if(null != name &&"" != name){
			if(name.equals("zhangsan") && password.equals("123")){
				session.setAttribute("username", name);
				return "redirect:/itemList.action";
			}
		}else{
			return "redirect:/skipToLogin.action";
		}
		return "redirect:/skipToLogin.action";
	}
	
	

}
